<template>
    <el-dialog v-model="visible" :title="dialogTitle" width="769px" append-to-body>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px">
            <el-form-item label="字典项名称" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入字典项名称" clearable />
            </el-form-item>
            <el-form-item label="字典项值" prop="value">
                <el-input v-model="ruleForm.value" placeholder="请输入字典项值" clearable />
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="ruleForm.sort" :min="1" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-switch v-model="ruleForm.status" inline-prompt active-text="正常" inactive-text="禁用" :active-value="1" :inactive-value="0" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="ruleForm.remark" placeholder="请输入备注" clearable :rows="2" type="textarea" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button size="default" @click="onCancel">取 消</el-button>
                <el-button type="primary" size="default" @click="onSubmit(ruleFormRef)">保 存</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { Dict } from '@/api/system/dict/types.ts'
import { saveDict, updateDict } from '@/api/system/dict/index.ts'
import { resetForm } from '@/utils/index.ts'

interface Props {
    typeCode: string
}
const props = defineProps<Props>()

const dialogTitle = ref('')
const visible = ref(false)
const ruleFormRef = ref<FormInstance>()
const defaultRuleForm: Dict.Form = {
    status: 1,
    sort: 1,
    typeCode: props.typeCode,
}
const ruleForm = reactive<Dict.Form>({
    status: 1,
    sort: 1,
    typeCode: '',
})

const rules = reactive<FormRules<Dict.Form>>({
    name: [{ required: true, message: '请输入字典项名称', trigger: 'blur' }],
    value: [{ required: true, message: '请输入字典项值', trigger: 'blur' }],
})

const onCancel = () => {
    visible.value = false
}

/** 打开弹窗 */
const openDialog = (item: Dict.Form) => {
    ruleFormRef.value?.clearValidate()
    if (item) {
        dialogTitle.value = '编辑字典项'
        Object.assign(ruleForm, item)
    } else {
        dialogTitle.value = '新增字典项'
        resetForm(ruleForm, defaultRuleForm)
    }
    visible.value = true
}

const emit = defineEmits(['refresh'])
const onSubmit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid) => {
        if (valid) {
            if (ruleForm.id) {
                updateDict(ruleForm).then(() => {
                    ElMessage.success('保存成功')
                    visible.value = false
                    emit('refresh')
                })
            } else {
                saveDict(ruleForm).then(() => {
                    ElMessage.success('保存成功')
                    visible.value = false
                    emit('refresh')
                })
            }
        }
    })
}

defineExpose({
    openDialog,
})
</script>
